package com.hamizhong.iflowers.web.base;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.hamizhong.iflowers.common.logger.MyLogger;
import com.hamizhong.iflowers.common.logger.MyLoggerFactory;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action基类
 * 
 * @author ZhongJiang
 *
 */
public class MyBaseWebAction extends ActionSupport {
	
	protected MyLogger logger = MyLoggerFactory.getLogger(this.getClass());
	
	protected HttpServletRequest getRQ(){
		return ServletActionContext.getRequest();
	}
	
	protected HttpServletResponse getRP(){
		return ServletActionContext.getResponse();
	}
	
	protected HttpSession getSSN(){
		return ServletActionContext.getRequest().getSession();
	}
	
	protected PrintWriter getPW() throws IOException {
		return ServletActionContext.getResponse().getWriter();
	}
	
	protected void setCharacterEncoding() {
		this.getRP().setCharacterEncoding("UTF-8");
		this.getRP().setCharacterEncoding("UTF-8");
	}
	
	protected int getPageNum(){
		int pg = 1;
		String pgS = this.getRQ().getParameter("page");
		try{
			pg = Integer.parseInt(pgS);
		}catch(Exception ex){}
		if(pg <= 0){
			pg = 1;
		}
		return pg;
	}
	
	protected int getPageCnt(int cnt,int eachPageCnt) {
		if(cnt == 0) {
			return 0;
		}else {
			return cnt%eachPageCnt<=0?cnt/eachPageCnt:cnt/eachPageCnt+1;
		}
	}
	
}
